package com.kingsoft.demo.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

public class Test2 {

	public static void test(){
		
		Connection con=null;
		Statement st=null;
		ResultSet rs=null;

		try {
			//1、加载驱动
			Class.forName("com.mysql.jdbc.Driver");
	  		//new oracle.jdbc.driver.OracleDriver();
			//2.打开数据库连接
			String url="jdbc:mysql://localhost:3306/test"; 
			String user="root";
			String password="admin";			
		    con=DriverManager.getConnection(url,user,password);
		  
			//3、获得语句分析容器
			st=con.createStatement();
			
		   //4、查询结果集
			String sql="select * from emp";
		    rs=st.executeQuery(sql);
		   
			while(rs.next()){
				//移动游标，如果该行存在，返回true，否则返回FALSE
				
			  System.out.println(rs.getString(1)+":"+rs.getString("ename")+":"+rs.getString(3));	

				
			}
			
			
		
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//关闭资源
			try {
			if(rs!=null)	
				rs.close();
			if(st!=null)	st.close();
			if(con!=null)	con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
		}
		
		
		
	}
	
	public static void main(String[] args) throws Exception{
		
		long  d = new Date().getTime();
		test();
		long d2 = new Date().getTime();
		System.out.println("执行时间："+(d2-d));
		
	
	} 
	
}
